﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ChoDiaOc_HK_WPF.Models;

namespace ChoDiaOc_HK_WPF.Controls {
    public partial class TiepNhanDangKyController {

        private static TiepNhanDangKyController controller = null;

        public static TiepNhanDangKyController GetInstance() {
            if (controller == null) {
                controller = new TiepNhanDangKyController();
            }
            return controller;
        }

        private bool isQCBao = false;

        public bool IsQCBao {
            get { return isQCBao; }
            set { isQCBao = value; }
        }
        private bool isQCBuom = false;

        public bool IsQCBuom {
            get { return isQCBuom; }
            set { isQCBuom = value; }
        }
        private bool isQCBang = false;

        public bool IsQCBang {
            get { return isQCBang; }
            set { isQCBang = value; }
        }

        private KHACHHANG khachHangSource = null;

        public KHACHHANG KhachHangSource {
            get { return khachHangSource; }
            set { khachHangSource = value; }
        }

        private DIAOC diaOcSource = null;

        public DIAOC DiaOcSource {
            get { return diaOcSource; }
            set { diaOcSource = value; }
        }

        private LOAIDIAOC loaiDiaOcSource = null;

        public LOAIDIAOC LoaiDiaOcSource {
            get { return loaiDiaOcSource; }
            set { loaiDiaOcSource = value; }
        }

        private QC_BANG quangCaoBangSource;

        public QC_BANG QuangCaoBangSource {
            get { return quangCaoBangSource; }
            set { quangCaoBangSource = value; }
        }

        private QC_BAO quangCaoBaoSource;

        public QC_BAO QuangCaoBaoSource {
            get { return quangCaoBaoSource; }
            set { quangCaoBaoSource = value; }
        }

        private BAO baoSource;

        public BAO BaoSource {
            get { return baoSource; }
            set { baoSource = value; }
        }

        private TOBUOM toBuomSource;

        public TOBUOM ToBuomSource {
            get { return toBuomSource; }
            set { toBuomSource = value; }
        }

        private LOAIBANG loaiBangSource;

        public LOAIBANG LoaiBangSource {
            get { return loaiBangSource; }
            set { loaiBangSource = value; }
        }

        private GIATIEN_QC_BANG giaTienQCBangSource;

        public GIATIEN_QC_BANG GiaTienQCBangSource {
            get { return giaTienQCBangSource; }
            set { giaTienQCBangSource = value; }
        }

        private GIATIEN_QC_BAO giaTienQCBaoSource;

        public GIATIEN_QC_BAO GiaTienQCBaoSource {
            get { return giaTienQCBaoSource; }
            set { giaTienQCBaoSource = value; }
        }

        private GIATIEN_TOBUOM giaTienQCBuomSource;

        public GIATIEN_TOBUOM GiaTienQCBuomSource {
            get { return giaTienQCBuomSource; }
            set { giaTienQCBuomSource = value; }
        }

        private List<BAO> listChonBao;

        public List<BAO> ListChonBao {
            get { return listChonBao; }
            set { listChonBao = value; }
        }

        private NOIDUNGQC noiDungQCSource;

        public NOIDUNGQC NoiDungQCSource {
            get { return noiDungQCSource; }
            set { noiDungQCSource = value; }
        }

        private PHIEUDANGKY phieuDangKySource;

        public PHIEUDANGKY PhieuDangKySource {
            get { return phieuDangKySource; }
            set {
                phieuDangKySource = value;
                phieuDangKySource.SoTien = (isQCBuom ? giaTienQCBuomSource.GiaTien : 0.0M) + (isQCBao ? giaTienQCBaoSource.GiaTien : 0.0M) + (isQCBang ? giaTienQCBangSource.GiaTien : 0.0M);
            }
        }

        private TiepNhanDangKyController() {

        }

        public String GenerateMaKH() {
            List<String> listMaKH = DatabaseWrapper.GetInstance().GetAllMaKhachHang();
            String generatedMaKH;
            do {
                generatedMaKH = "";

                var chars = "FGDF8G7S5D7FWHJRTGEWO8652958645869SG6F9D8G75XC7V46X436D3F64HJ3H52GHMBVUHJXVBSDIY";
                var random = new Random();
                var result = new string(
                    Enumerable.Repeat(chars, 5)
                              .Select(s => s[random.Next(s.Length)])
                              .ToArray());
                generatedMaKH += result;
            } while (listMaKH.Contains(generatedMaKH));
            return generatedMaKH;
        }

        public String GenerateMaDiaOc() {
            List<String> listMaDiaOc = DatabaseWrapper.GetInstance().GetAllMaDiaOc();
            String generatedMaDiaOc;
            do {
                generatedMaDiaOc = "";

                var chars = "FGDF8G7S5D7FWHJRTGEWO8652958645869SG6F9D8G75XC7V46X436D3F64HJ3H52GHMBVUHJXVBSDIY";
                var random = new Random();
                var result = new string(
                    Enumerable.Repeat(chars, 5)
                              .Select(s => s[random.Next(s.Length)])
                              .ToArray());
                generatedMaDiaOc += result;
            } while (listMaDiaOc.Contains(generatedMaDiaOc));
            return generatedMaDiaOc;
        }

        public String GenerateMaQC_BANG() {
            List<String> listMaDiaOc = DatabaseWrapper.GetInstance().GetAllMaQCBang();
            String generatedMaDiaOc;
            do {
                generatedMaDiaOc = "";

                var chars = "FGDF8G7S5D7FWHJRTGEWO8652958645869SG6F9D8G75XC7V46X436D3F64HJ3H52GHMBVUHJXVBSDIY";
                var random = new Random();
                var result = new string(
                    Enumerable.Repeat(chars, 5)
                              .Select(s => s[random.Next(s.Length)])
                              .ToArray());
                generatedMaDiaOc += result;
            } while (listMaDiaOc.Contains(generatedMaDiaOc));
            return generatedMaDiaOc;
        }

        public String GenerateMaQC_BAO() {
            List<String> listMaDiaOc = DatabaseWrapper.GetInstance().GetAllMaQCBao();
            String generatedMaDiaOc;
            do {
                generatedMaDiaOc = "";

                var chars = "FGDF8G7S5D7FWHJRTGEWO8652958645869SG6F9D8G75XC7V46X436D3F64HJ3H52GHMBVUHJXVBSDIY";
                var random = new Random();
                var result = new string(
                    Enumerable.Repeat(chars, 5)
                              .Select(s => s[random.Next(s.Length)])
                              .ToArray());
                generatedMaDiaOc += result;
            } while (listMaDiaOc.Contains(generatedMaDiaOc));
            return generatedMaDiaOc;
        }

        public String GenerateMaQC_TOBUOM() {
            List<String> listMaDiaOc = DatabaseWrapper.GetInstance().GetAllMaQCBuom();
            String generatedMaDiaOc;
            do {
                generatedMaDiaOc = "";

                var chars = "FGDF8G7S5D7FWHJRTGEWO8652958645869SG6F9D8G75XC7V46X436D3F64HJ3H52GHMBVUHJXVBSDIY";
                var random = new Random();
                var result = new string(
                    Enumerable.Repeat(chars, 5)
                              .Select(s => s[random.Next(s.Length)])
                              .ToArray());
                generatedMaDiaOc += result;
            } while (listMaDiaOc.Contains(generatedMaDiaOc));
            return generatedMaDiaOc;
        }

        public String GenerateMaGIATIEN_QCBANG() {
            List<String> listMaDiaOc = DatabaseWrapper.GetInstance().GetAllMaGiaTienQCBang();
            String generatedMaDiaOc;
            do {
                generatedMaDiaOc = "";

                var chars = "FGDF8G7S5D7FWHJRTGEWO8652958645869SG6F9D8G75XC7V46X436D3F64HJ3H52GHMBVUHJXVBSDIY";
                var random = new Random();
                var result = new string(
                    Enumerable.Repeat(chars, 5)
                              .Select(s => s[random.Next(s.Length)])
                              .ToArray());
                generatedMaDiaOc += result;
            } while (listMaDiaOc.Contains(generatedMaDiaOc));
            return generatedMaDiaOc;
        }

        public String GenerateMaGIATIEN_QCBAO() {
            List<String> listMaDiaOc = DatabaseWrapper.GetInstance().GetAllMaGiaTienQCBao();
            String generatedMaDiaOc;
            do {
                generatedMaDiaOc = "";

                var chars = "FGDF8G7S5D7FWHJRTGEWO8652958645869SG6F9D8G75XC7V46X436D3F64HJ3H52GHMBVUHJXVBSDIY";
                var random = new Random();
                var result = new string(
                    Enumerable.Repeat(chars, 5)
                              .Select(s => s[random.Next(s.Length)])
                              .ToArray());
                generatedMaDiaOc += result;
            } while (listMaDiaOc.Contains(generatedMaDiaOc));
            return generatedMaDiaOc;
        }

        public String GenerateMaGIATIEN_QCTOBUOM() {
            List<String> listMaDiaOc = DatabaseWrapper.GetInstance().GetAllMaGiaTienQCBuom();
            String generatedMaDiaOc;
            do {
                generatedMaDiaOc = "";

                var chars = "FGDF8G7S5D7FWHJRTGEWO8652958645869SG6F9D8G75XC7V46X436D3F64HJ3H52GHMBVUHJXVBSDIY";
                var random = new Random();
                var result = new string(
                    Enumerable.Repeat(chars, 5)
                              .Select(s => s[random.Next(s.Length)])
                              .ToArray());
                generatedMaDiaOc += result;
            } while (listMaDiaOc.Contains(generatedMaDiaOc));
            return generatedMaDiaOc;
        }

        public String GenerateMaCHITIETBAO() {
            List<String> listMaDiaOc = DatabaseWrapper.GetInstance().GetAllMaChiTietBao();
            String generatedMaDiaOc;
            do {
                generatedMaDiaOc = "";

                var chars = "FGDF8G7S5D7FWHJRTGEWO8652958645869SG6F9D8G75XC7V46X436D3F64HJ3H52GHMBVUHJXVBSDIY";
                var random = new Random();
                var result = new string(
                    Enumerable.Repeat(chars, 5)
                              .Select(s => s[random.Next(s.Length)])
                              .ToArray());
                generatedMaDiaOc += result;
            } while (listMaDiaOc.Contains(generatedMaDiaOc));
            return generatedMaDiaOc;
        }

        public String GenerateMaNOIDUNG() {
            List<String> listMaDiaOc = DatabaseWrapper.GetInstance().GetAllMaNoiDung();
            String generatedMaDiaOc;
            do {
                generatedMaDiaOc = "";

                var chars = "FGDF8G7S5D7FWHJRTGEWO8652958645869SG6F9D8G75XC7V46X436D3F64HJ3H52GHMBVUHJXVBSDIY";
                var random = new Random();
                var result = new string(
                    Enumerable.Repeat(chars, 5)
                              .Select(s => s[random.Next(s.Length)])
                              .ToArray());
                generatedMaDiaOc += result;
            } while (listMaDiaOc.Contains(generatedMaDiaOc));
            return generatedMaDiaOc;
        }

        private bool isKhachHangCu = false;

        public bool IsKhachHangCu {
            get { return isKhachHangCu; }
            set { isKhachHangCu = value; }
        }

        public bool GhiNhanKhachHang() {
            return DatabaseWrapper.GetInstance().AddNewKhachHang(khachHangSource, isKhachHangCu);
        }

        public bool GhiNhanDiaOc() {
            diaOcSource.MaLoaiDiaOc = loaiDiaOcSource.MaLoaiDiaOc;
            return DatabaseWrapper.GetInstance().AddNewDiaOc(diaOcSource);
        }

        public bool GhiNhanQC_BANG() {
            quangCaoBangSource.MaND = noiDungQCSource.MaND;
            quangCaoBangSource.MaGiaTien = giaTienQCBangSource.MaGiaTien;
            quangCaoBangSource.MaPhieuDangKy = phieuDangKySource.MaPhieuDangKy;
            giaTienQCBangSource.MaLoaiBang = loaiBangSource.MaLoaiBang;
            bool result = DatabaseWrapper.GetInstance().AddNewGiaTienQCBang(giaTienQCBangSource) &&
                DatabaseWrapper.GetInstance().AddNewQCBang(quangCaoBangSource);
            return result;
        }

        public bool GhiNhanQC_BAO() {
            bool result = true;
            quangCaoBaoSource.MaPhieuDangKy = phieuDangKySource.MaPhieuDangKy;
            quangCaoBaoSource.MaND = noiDungQCSource.MaND;
            quangCaoBaoSource.MaGiaTien = giaTienQCBaoSource.MaGiaTien;
            result = result && DatabaseWrapper.GetInstance().AddNewGiaTienQCBao(giaTienQCBaoSource);
            result = result && DatabaseWrapper.GetInstance().AddNewQCBao(quangCaoBaoSource);
            CHITIETBAO ct = null;
            foreach (BAO b in listChonBao) {
                ct = new CHITIETBAO();
                ct.MaCT = GenerateMaCHITIETBAO();
                ct.MaQCBao = quangCaoBaoSource.MaQCBao;
                ct.MaBao = b.MaBao;
                result = result && DatabaseWrapper.GetInstance().AddNewChiTietBao(ct);
            }
            return result;
        }

        public bool GhiNhanQC_TOBUOM() {
            bool result = true;
            toBuomSource.MaGiaTen = giaTienQCBuomSource.MaGiaTien;
            toBuomSource.MaPhieuDangKy = phieuDangKySource.MaPhieuDangKy;
            toBuomSource.MaND = noiDungQCSource.MaND;
            result = result && DatabaseWrapper.GetInstance().AddNewGiaTienQCBuom(giaTienQCBuomSource);
            result = result && DatabaseWrapper.GetInstance().AddQCBuom(toBuomSource);
            return result;
        }

        public bool GhiNhanNoiDungQC() {
            return DatabaseWrapper.GetInstance().AddNewNoiDungQC(noiDungQCSource);
        }

        public bool GhiNhanPhieuDangKy() {

            bool result = true;

            // Ghi nhan KHACHHANG + MaKH trong PHIEUDANGKY
            result = result && GhiNhanKhachHang();
            phieuDangKySource.MaKH = khachHangSource.MaKH;

            // Ghi nhan DIAOC + MaDiaOc trong PHIEUDANGKY
            result = result && GhiNhanDiaOc();
            phieuDangKySource.MaDiaOc = diaOcSource.MaDiaOc;

            // Ghi nhan NOIDUNGQC
            result = result && GhiNhanNoiDungQC();

            // Ghi nhan PHIEUDANGKY
            result = result && DatabaseWrapper.GetInstance().AddNewPhieuDangKy(phieuDangKySource);

            // Ghi nhan QCBANG + MaPhieuDangKy, MaND trong QC_BANG
            if (isQCBang) result = result && GhiNhanQC_BANG();

            // Ghi nhan QCBAO + MaPhieuDangKy, MaND trong QC_BAO
            if (isQCBao) result = result && GhiNhanQC_BAO();

            // Ghi nhan QCBUOM + MaPhieuDangKy, MaND trong QC_BUOM
            if (isQCBuom) result = result && GhiNhanQC_TOBUOM();

            return result;
        }

        public string GenerateMaPHIEUDANGKY() {
            List<String> listMaKH = DatabaseWrapper.GetInstance().GetAllMaPhieuThu();
            String generatedMaKH;
            do {
                generatedMaKH = "";

                var chars = "FGDF8G7S5D7FWHJRTGEWO8652958645869SG6F9D8G75XC7V46X436D3F64HJ3H52GHMBVUHJXVBSDIY";
                var random = new Random();
                var result = new string(
                    Enumerable.Repeat(chars, 5)
                              .Select(s => s[random.Next(s.Length)])
                              .ToArray());
                generatedMaKH += result;
            } while (listMaKH.Contains(generatedMaKH));
            return generatedMaKH;
        }

        public string GenerateMaDANGNHAP() {
            List<String> listMaKH = DatabaseWrapper.GetInstance().GetAllMaDangNhap();
            String generatedMaKH;
            do {
                generatedMaKH = "";

                var chars = "FGDF8G7S5D7FWHJRTGEWO8652958645869SG6F9D8G75XC7V46X436D3F64HJ3H52GHMBVUHJXVBSDIY";
                var random = new Random();
                var result = new string(
                    Enumerable.Repeat(chars, 5)
                              .Select(s => s[random.Next(s.Length)])
                              .ToArray());
                generatedMaKH += result;
            } while (listMaKH.Contains(generatedMaKH));
            return generatedMaKH;
        }

        public bool DangKyTaiKhoanKH() {
            return DatabaseWrapper.GetInstance().UpdateDangNhap(khachHangSource.HoTen, khachHangSource.MaKH);
        }


        public string GenerateMaLOAIBANG() {
            List<String> listMaDiaOc = DatabaseWrapper.GetInstance().GetAllMaLoaiBang();
            String generatedMaDiaOc;
            do {
                generatedMaDiaOc = "";

                var chars = "FGDF8G7S5D7FWHJRTGEWO8652958645869SG6F9D8G75XC7V46X436D3F64HJ3H52GHMBVUHJXVBSDIY";
                var random = new Random();
                var result = new string(
                    Enumerable.Repeat(chars, 5)
                              .Select(s => s[random.Next(s.Length)])
                              .ToArray());
                generatedMaDiaOc += result;
            } while (listMaDiaOc.Contains(generatedMaDiaOc));
            return generatedMaDiaOc;
        }

        public string GenerateMaBAO() {
            List<String> listMaDiaOc = DatabaseWrapper.GetInstance().GetAllMaBao();
            String generatedMaDiaOc;
            do {
                generatedMaDiaOc = "";

                var chars = "FGDF8G7S5D7FWHJRTGEWO8652958645869SG6F9D8G75XC7V46X436D3F64HJ3H52GHMBVUHJXVBSDIY";
                var random = new Random();
                var result = new string(
                    Enumerable.Repeat(chars, 5)
                              .Select(s => s[random.Next(s.Length)])
                              .ToArray());
                generatedMaDiaOc += result;
            } while (listMaDiaOc.Contains(generatedMaDiaOc));
            return generatedMaDiaOc;
        }

        public string GenerateMaLOAIDIAOC() {
            List<String> listMaDiaOc = DatabaseWrapper.GetInstance().GetAllMaLoaiDiaOc();
            String generatedMaDiaOc;
            do {
                generatedMaDiaOc = "";

                var chars = "FGDF8G7S5D7FWHJRTGEWO8652958645869SG6F9D8G75XC7V46X436D3F64HJ3H52GHMBVUHJXVBSDIY";
                var random = new Random();
                var result = new string(
                    Enumerable.Repeat(chars, 5)
                              .Select(s => s[random.Next(s.Length)])
                              .ToArray());
                generatedMaDiaOc += result;
            } while (listMaDiaOc.Contains(generatedMaDiaOc));
            return generatedMaDiaOc;
        }

    }
}
